package com.jjwxc.jwjskandriod.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.widget.ToggleRadioButton;

import java.util.ArrayList;
import java.util.List;

public class TagGroupView extends RadioGroup {
    private LayoutInflater mInflater;
    //标签的值集合
    private List<String> tags;
    //radioButton返回结果，选中了哪个
    private int mRadioButtonCheck;

    private List<Integer> radioButtonDefaultId = new ArrayList<>();
    private int mRadioButtonId;
    //checkBox返回结果，选中了几个
    private List<String> mCheckBoxCheck = new ArrayList<>();
    private List<Integer> mCheckBoxIdList = new ArrayList<>();
    private List<String> mCheckBoxIds = new ArrayList<>();
    //@hl 记录是第几行，需要做如果是第三行开始，则需要开始隐藏这些View
    private static final int MAX_SHOW_COUNT = 2;
    //计算行数
    private int line_cout = 0;
    private boolean bFirtLine = true;
    private int totalShowHeight, totalFolderShowHeight = 0;
    //@hl 控制是否显示所有
    private boolean bShowAll = true;

    public TagGroupView(Context context) {
        super(context);
    }

    public TagGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //调用ViewGroup的方法，测量子view
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //最大的宽
        int maxWidth = 0;
        //累计的高
        int totalHeight = 0;

        //当前这一行的累计行宽
        int lineWidth = 0;
        //当前这行的最大行高
        int maxLineHeight = 0;
        //用于记录换行前的行宽和行高
        int oldHeight;
        int oldWidth;

        int count = getChildCount();
        //假设 widthMode和heightMode都是AT_MOST
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            //得到这一行的最高
            oldHeight = maxLineHeight;
            //当前最大宽度
            oldWidth = maxWidth;

            int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
                //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth
                maxWidth = Math.max(lineWidth, oldWidth);
                //重置宽度
                lineWidth = deltaX;

                // 额外的margin_top间距，一旦换行，也就是从第二行开始就需要增加margin_top，记得累积高度
                int extern_margin_top = params.topMargin;

                //累加高度
                totalHeight += oldHeight;
                //重置行高,当前这个View，属于下一行，因此当前最大行高为这个child的高度加上margin
                maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin + extern_margin_top;

                //@hl 两行之后只显示两行的高度，不再增加换行高度
                ++line_cout;
                if (line_cout <= MAX_SHOW_COUNT) {
                    totalFolderShowHeight = totalHeight;
                }
            } else {
                //不换行，累加宽度
                lineWidth += deltaX;
                //不换行，计算行最高
                int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                maxLineHeight = Math.max(maxLineHeight, deltaY);
            }
            if (i == count - 1) {
                //前面没有加上下一行的高，如果是最后一行，还要再叠加上最后一行的最高的值
                totalHeight += maxLineHeight;
                //计算最后一行和前面的最宽的一行比较
                maxWidth = Math.max(lineWidth, oldWidth);
            }
        }

        //加上当前容器的padding值
        maxWidth += getPaddingLeft() + getPaddingRight();
        totalHeight += getPaddingTop() + getPaddingBottom();
        //@hl 保存用于全部展开使用
        totalShowHeight = totalHeight;
        if (bShowAll) {
            setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
                    heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);
        } else {
            //@hl 高度如果超过两行，则只显示两行
            setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
                    heightMode == MeasureSpec.EXACTLY ? heightSize :
                            (totalFolderShowHeight > 0 ? totalFolderShowHeight : totalShowHeight));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        //pre为前面所有的child的相加后的位置
        int preLeft = getPaddingLeft();
        int preTop = getPaddingTop();
        //记录每一行的最高值
        int maxHeight = 0;
        //@hl 第一行不能加，之后需要添加margintop
        int extern_margin_top = 0;
        // 初始化 - 这个布局会回调几次
        bFirtLine = true;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            int left;
            //@hl int extern_margin_top = 0;
            //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度，就换行。
            if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
                //重置
                preLeft = getPaddingLeft();
                //要选择child的height最大的作为设置
                preTop = preTop + maxHeight;
                maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
                //left坐标 - 刚换行的第一个不增加leftmargin
                left = preLeft;
                // 额外的margin_top间距，一旦换行，也就是从第二行开始就需要增加margin_top，记得累积
                extern_margin_top = extern_margin_top + params.topMargin;

                bFirtLine = false;
            } else { //不换行,计算最大高度
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
                //@hl 换行第一次的第二个控件不能再加一次preLeft
                if (!bFirtLine) {
                    //left坐标
                    left = preLeft;
                } else {
                    //left坐标
                    left = preLeft + params.leftMargin;
                }
            }
            //left坐标
            //@hl int left = preLeft + params.leftMargin;
            //top坐标
            int top = preTop + extern_margin_top; //params.topMargin;
            int right = left + child.getMeasuredWidth();
            int bottom = top + child.getMeasuredHeight();
            //为子view布局
            child.layout(left, top, right, bottom);
            //计算布局结束后，preLeft的值
            preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
        }
    }

    public void setRadioButtonTags(List<String> tagList, int id, boolean icon) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                RadioButton tagView = (RadioButton) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i));
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                if (icon && i < 3) {
                    Drawable left = getResources().getDrawable(R.drawable.prosperous_icon);
                    left.setBounds(0, 0, FFUtils.getPx(18), FFUtils.getPx(18));//必须设置图片的大小否则没有作用
                    tagView.setCompoundDrawables(left, null, null, null);//设置图片left这里如果是右边就放到第二个参数里面依次对应
                }
                int finalI = i;
                tagView.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (b) {
                        mRadioButtonCheck = (finalI + 1);
                        mRadioButtonId = tagView.getId();
                    }
                });

                addView(tagView);
            }
        }
        postInvalidate();
    }

    public void setRadioButtonTags(List<String> tagList, int id) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                RadioButton tagView = (RadioButton) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i));
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                int finalI = i;
                tagView.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (b) {
                        mRadioButtonCheck = (finalI + 1);
                        mRadioButtonId = tagView.getId();
                    }
                });

                addView(tagView);
            }
        }
        postInvalidate();
    }

    public void setRadioButtonTagsByScren(List<String> tagList, int id) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                ToggleRadioButton tagView = (ToggleRadioButton) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(FFUtils.getPx(75), FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i));
                tagView.setLines(1);
                tagView.setEllipsize(TextUtils.TruncateAt.END);
                tagView.setMaxEms(4);
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                int finalI = i;
                tagView.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (b) {
                        mRadioButtonCheck = (finalI + 1);
                        mRadioButtonId = tagView.getId();
                    }
                });

                addView(tagView);
            }
        }
        postInvalidate();
    }

    public void setRadioButtonTagsByLeft(List<String> tagList, int id) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                RadioButton tagView = (RadioButton) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(FFUtils.getPx(75), FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i));
                tagView.setLines(1);
                tagView.setEllipsize(TextUtils.TruncateAt.END);
                tagView.setMaxEms(4);
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                int finalI = i;
                tagView.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (b) {
                        mRadioButtonCheck = (finalI + 1);
                        mRadioButtonId = tagView.getId();
                    } else {
                        mRadioButtonCheck = 0;
                        mRadioButtonId = tagView.getId();
                    }
                });

                addView(tagView);
            }
        }
        postInvalidate();
    }

    public void setRadioButtonByFlg(List<String> tagList, int id, int flg) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            int w = flg + tags.size();
            for (int i = flg; i < w; i++) {
                RadioButton tagView = (RadioButton) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(FFUtils.getPx(75), FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i - flg));
                tagView.setLines(1);
                tagView.setEllipsize(TextUtils.TruncateAt.END);
                tagView.setMaxEms(4);
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                addView(tagView);
            }
        }
        postInvalidate();
    }

    public void setCheckBoxTags(List<String> tagList, List<String> tagId, int id) {
        this.tags = tagList;
        this.removeAllViews();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                CheckBox tagView = (CheckBox) mInflater.inflate(id, null);
                tagView.setId(i);
                MarginLayoutParams layoutParams = new MarginLayoutParams(FFUtils.getPx(75), FFUtils.getPx(26));
                layoutParams.setMargins(10, 10, 10, 10);
                tagView.setLayoutParams(layoutParams);
                tagView.setPadding(25, 0, 25, 0);
                tagView.setText(tags.get(i));
                tagView.setLines(1);
                tagView.setEllipsize(TextUtils.TruncateAt.END);
                tagView.setMaxEms(4);
                tagView.setTag(i);
                radioButtonDefaultId.add(tagView.getId());
                int finalI = i;
                tagView.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (b) {
                        mCheckBoxCheck.add(tagView.getText().toString().trim());
                        mCheckBoxIdList.add(tagView.getId());
                        mCheckBoxIds.add(tagId.get(finalI));
                    } else {
                        mCheckBoxCheck.remove(tagView.getText().toString().trim());
                        mCheckBoxIdList.remove(Integer.valueOf(tagView.getId()));
                        mCheckBoxIds.remove(tagId.get(finalI));
                    }
                });

                addView(tagView);
            }
        }
        postInvalidate();
    }

    /**
     * 点击显示其他控件
     */
    public void requestShowMeasure(boolean _bShowAll) {
        bShowAll = _bShowAll;
        requestLayout();
    }

    public int getRadioButtonCheck() {
        return mRadioButtonCheck;
    }

    public List<String> getCheckBoxCheck() {
        return mCheckBoxCheck;
    }


    /**
     * 设置RadioButton默认选中某一个
     */
    public void checkSelecteDefault(int num) {
        List<Integer> list = new ArrayList<>(radioButtonDefaultId);
        if (list.size() > 0 && num < list.size() && num >= 0) {
            for (int i = 0; i < list.size(); i++) {
                RadioButton radioButton = (RadioButton) findViewById(list.get(i));
                if (i == num) {
                    radioButton.setChecked(true);
                } else {
                    radioButton.setChecked(false);
                }
            }

        }
    }

    /**
     * 设置RadioButton默认选中某一个
     * flg 是起始点
     */
    public void checkSelecteDefault(int num, int flg) {
        List<Integer> list = new ArrayList<>(radioButtonDefaultId);
        if (list.size() + flg > 0 && num < list.size() + flg && num >= 0) {
            int w = flg + list.size();
            for (int i = flg; i < w; i++) {
                RadioButton radioButton = (RadioButton) findViewById(list.get(i - flg));
                if (i == num) {
                    radioButton.setChecked(true);
                } else {
                    radioButton.setChecked(false);
                }
            }

        }
    }

    public void selecteCheckBoxStateDefault(List<Integer> num) {
        List<Integer> list = new ArrayList<>(radioButtonDefaultId);
        for (int w = 0; w < num.size(); w++) {
            if (list.size() > 0 && num.get(w) < list.size() && num.get(w) >= 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == num.get(w)) {
                        CheckBox checkBox = (CheckBox) findViewById(list.get(i));
                        checkBox.setChecked(true);
                    }
                }

            }
        }


    }

    /**
     * 获取CheckBox选中的ID
     * @return
     */
    public List<String> checkBoxSelect() {
        return mCheckBoxIds;
    }

    /**
     * CheckBox全部选中
     */
    public void selecteCheckBoxState() {
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                CheckBox checkBox = (CheckBox) getChildAt(i);
                checkBox.setChecked(true);
            }

        }
    }

    /**
     * CheckBox全部取消
     */
    public void clearCheckBoxState() {
        List<Integer> list = new ArrayList<>(mCheckBoxIdList);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                CheckBox checkBox = (CheckBox) findViewById(list.get(i));
                checkBox.setChecked(false);
            }

        }
    }

    /**
     * 是否超过两行
     *
     * @return
     */
    public boolean bHasMultiline() {
        return line_cout >= MAX_SHOW_COUNT;
    }
}
